package com.example.week2.mvp.view;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.week2.MainActivity;
import com.example.week2.R;
import com.example.week2.Retrofit.Api;
import com.example.week2.Retrofit.RetrofitManager;
import com.example.week2.Retrofit.UserBean;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.et1)
    EditText et1;
    @BindView(R.id.et2)
    EditText et2;
    @BindView(R.id.bt)
    Button bt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        String s1 = intent.getStringExtra("s1");
        String s2 = intent.getStringExtra("s2");
        et1.setText(s1);
        et2.setText(s2);

    }

//    7.自定义封装Observer回调
    @OnClick(R.id.bt)
    public void onViewClicked() {
        String trim = et1.getText().toString().trim();
        String trim1 = et2.getText().toString().trim();

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("username",trim);
        hashMap.put("user_password",trim1);
        OkGo.<String>post("http://172.81.227.127:8055/swagger-ui.html#!/user45controller/loginUserUsingPOST")
                .params(hashMap)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Animation animation = AnimationUtils.loadAnimation(LoginActivity.this, R.anim.a_animation);
                        animation.setDuration(1000);
                        animation.start();
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);

                    }
                });



//        RetrofitManager.getInstance().setBaseUrl("http://172.81.227.127:8055/swagger-ui.html#!/user45controller/");
//        RetrofitManager.getInstance().getRetrofit()
//                .create(Api.class)
//                .getlogin(trim,trim1)
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribeOn(Schedulers.io())
//                .subscribe(new Observer<UserBean>() {
//                    @Override
//                    public void onSubscribe(Disposable d) {
//
//                    }
//
//                    @Override
//                    public void onNext(UserBean userBean) {
//                        Toast.makeText(LoginActivity.this, ""+userBean, Toast.LENGTH_SHORT).show();
//                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
//                        startActivity(intent);
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//
//                    }
//
//                    @Override
//                    public void onComplete() {
//
//                    }
//                });
    }
}
